package com.fruit.web.vo.genl;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;


import com.fruit.service.IProductUnitService;
import com.fruit.utils.MessageFactory;
import com.fruit.utils.SpringFacesUtil;
import com.fruit.web.bo.impl.Productunit;
import com.fruit.web.constant.EjdConstants;
import com.fruit.web.constant.SpringBeanConstants;

@ManagedBean(name="existProductUnit")
@SessionScoped
public class ExistProductUnitBean implements Serializable {

	private static final long serialVersionUID = 5054087821033164859L;
	
	private SelectItem[] unitItems;

	public SelectItem[] getUnitItems() {
		return unitItems;
	}

	public void setUnitItems(SelectItem[] unitItems) {
		this.unitItems = unitItems;
	}
	public ExistProductUnitBean() {
		
		IProductUnitService<Productunit,Long> productUnitService = (IProductUnitService<Productunit,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.PRODUCTUNIT_SERVICE_BEAN_NAME);
		productUnitService.setEntityClass(Productunit.class);
		productUnitService.setPKClass(Long.class);
		List<Productunit> productUnits = productUnitService.getAll("serialNumber", true);
		SelectItem[] tempUnitItems = new SelectItem[productUnits.size()];
		if (null != productUnits && !(productUnits.isEmpty())) {
			for (int i=0; i< tempUnitItems.length; i++) {
				Productunit productUnit = (Productunit) productUnits.get(i);
				String unitNameN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,productUnit.getUnitName(), FacesMessage.SEVERITY_ERROR).getDetail();
				tempUnitItems[i] = new SelectItem(productUnit.getUnitId(),unitNameN18);
			}
		}
		this.setUnitItems(tempUnitItems);
	}
}
